package ddz;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Card extends JPanel implements MouseListener, Comparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * @param args
	 */
	public static Image images[] = {
			new ImageIcon("View\\heart.png").getImage(),
			new ImageIcon("View\\blackHeart.png").getImage(),
			new ImageIcon("View\\square.png").getImage(),
			new ImageIcon("View\\flower.png").getImage()

	};

	private int type, num;

	private boolean covered = true;

	private boolean selected = false;

	private boolean isVertical = true;

	private boolean sent = false;

	public Card(int _type, int _num) {
		this(_type, _num, true);
	}

	public Card(int _type, int _num, boolean _isVertical) {
		type = _type;
		num = _num;
		if (_isVertical)
			toVertical();
		else {
			toHorizental();
		}
		addMouseListener(this);
	}

	public Card() {
		this(0, 3);
	}

	public void uncover() {
		if (covered) {
			covered = false;
			repaint();
		}
	}

	public void select() {
		selected = true;
		setLocation(getX(), getY() - 20);
		Resources.SELECT_MUSIC.play();
	}

	public void send() {
		sent = true;
	}

	public void unSelect() {
		selected = false;
		setLocation(getX(), getY() + 20);
	}

	public boolean isSelected() {
		return selected;
	}

	public void cover() {
		if (!covered) {
			covered = true;
			repaint();
		}
	}

	public boolean isVertical() {
		return isVertical;
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
		int w = getWidth(), h = getHeight();
		g2d.drawRoundRect(0, 0, w - 1, h - 1, 0, 0);
		if (!isVertical) {
			g2d.translate(0, getHeight());
			g2d.rotate(-Math.PI / 2);
			w = getHeight();
			h = getWidth();
			// g2d.drawImage(new
			// ImageIcon("View\\pgyb.jpg").getImage(),2,2,getHeight()-4,getWidth()-4,null);
			// return;
		}

		if (covered) {
			g2d.drawImage(new ImageIcon("View\\pgyb.jpg").getImage(), 2, 2,
					w - 4, h - 4, null);
			return;
		}

		g2d.setFont(new Font("Times New Roman", Font.BOLD, 20));
		if (type == 0 || type == 2)
			g2d.setColor(Color.red);
		switch (num) {
		case 3:
			g2d.drawString("3", 6, 20);

			g2d.drawImage(images[type], 6, 22, 12, 12, this);
			g2d.drawImage(images[type], 37, 15, 20, 25, null);
			g2d.drawImage(images[type], 37, 60, 20, 25, null);
			g2d.translate(w, h);
			g2d.rotate(Math.PI);
			g2d.drawString("3", 6, 20);
			g2d.drawImage(images[type], 6, 22, 12, 12, this);
			g2d.drawImage(images[type], 37, 15, 20, 25, null);
			break;
		case 4:
			g2d.drawString("4", 6, 20);
			g2d.drawImage(images[type], 7, 25, 12, 12, this);

			g2d.drawImage(images[type], 18, 18, 22, 23, null);
			g2d.drawImage(images[type], 60, 18, 22, 23, null);

			g2d.translate(w, h);
			g2d.rotate(Math.PI);

			g2d.drawString("4", 0, 20);
			g2d.drawImage(images[type], 1, 25, 12, 12, this);

			g2d.drawImage(images[type], 12, 18, 22, 23, null);
			g2d.drawImage(images[type], 54, 18, 22, 23, null);

			break;
		case 5:
			g2d.drawString("5", 6, 20);
			g2d.drawImage(images[type], 7, 25, 12, 12, this);

			g2d.drawImage(images[type], 18, 18, 22, 23, null);
			g2d.drawImage(images[type], 60, 18, 22, 23, null);
			g2d.drawImage(images[type], 40, 60, 22, 23, null);
			g2d.translate(w, h);
			g2d.rotate(Math.PI);

			g2d.drawString("5", 0, 20);
			g2d.drawImage(images[type], 1, 25, 12, 12, this);

			g2d.drawImage(images[type], 12, 18, 22, 23, null);
			g2d.drawImage(images[type], 54, 18, 22, 23, null);

			break;
		case 6:
			g2d.drawString("6", 6, 20);
			g2d.drawImage(images[type], 7, 25, 12, 12, this);

			g2d.drawImage(images[type], 18, 18, 22, 23, null);
			g2d.drawImage(images[type], 60, 18, 22, 23, null);
			g2d.drawImage(images[type], 18, 60, 22, 23, null);
			g2d.drawImage(images[type], 60, 60, 22, 23, null);
			g2d.translate(w, h);
			g2d.rotate(Math.PI);

			g2d.drawString("6", 0, 20);
			g2d.drawImage(images[type], 1, 25, 12, 12, this);

			g2d.drawImage(images[type], 12, 18, 22, 23, null);
			g2d.drawImage(images[type], 54, 18, 22, 23, null);

			break;
		case 7:
			g2d.drawString("7", 6, 20);
			g2d.drawImage(images[type], 7, 25, 12, 12, this);

			g2d.drawImage(images[type], 18, 18, 22, 23, null);
			g2d.drawImage(images[type], 60, 18, 22, 23, null);
			g2d.drawImage(images[type], 18, 60, 22, 23, null);
			g2d.drawImage(images[type], 60, 60, 22, 23, null);
			g2d.drawImage(images[type], 40, 40, 22, 23, null);
			g2d.translate(w, h);
			g2d.rotate(Math.PI);

			g2d.drawString("7", 0, 20);
			g2d.drawImage(images[type], 1, 25, 12, 12, this);

			g2d.drawImage(images[type], 12, 18, 22, 23, null);
			g2d.drawImage(images[type], 54, 18, 22, 23, null);

			break;
		case 8:
			g2d.drawString("8", 6, 20);
			g2d.drawImage(images[type], 7, 25, 12, 12, this);

			g2d.drawImage(images[type], 18, 18, 22, 23, null);
			g2d.drawImage(images[type], 18, 45, 22, 23, null);
			g2d.drawImage(images[type], 60, 18, 22, 23, null);
			g2d.drawImage(images[type], 60, 45, 22, 23, null);

			g2d.translate(w, h);
			g2d.rotate(Math.PI);
			g2d.drawString("8", 5, 20);
			g2d.drawImage(images[type], 2, 25, 12, 12, this);

			g2d.drawImage(images[type], 14, 18, 22, 23, null);
			g2d.drawImage(images[type], 14, 45, 22, 23, null);
			g2d.drawImage(images[type], 56, 18, 22, 23, null);
			g2d.drawImage(images[type], 56, 45, 22, 23, null);
			break;
		case 9:
			g2d.drawString("9", 6, 20);
			g2d.drawImage(images[type], 7, 25, 12, 12, this);
			g2d.drawImage(images[type], 39, 60, 22, 23, null);
			g2d.drawImage(images[type], 18, 18, 22, 23, null);
			g2d.drawImage(images[type], 18, 45, 22, 23, null);
			g2d.drawImage(images[type], 60, 18, 22, 23, null);
			g2d.drawImage(images[type], 60, 45, 22, 23, null);

			g2d.translate(w, h);
			g2d.rotate(Math.PI);
			g2d.drawString("9", 5, 20);
			g2d.drawImage(images[type], 2, 25, 12, 12, this);

			g2d.drawImage(images[type], 14, 18, 22, 23, null);
			g2d.drawImage(images[type], 14, 45, 22, 23, null);
			g2d.drawImage(images[type], 56, 18, 22, 23, null);
			g2d.drawImage(images[type], 56, 45, 22, 23, null);
			break;
		case 10:
			g2d.drawString("10", 2, 20);
			g2d.drawImage(images[type], 7, 25, 12, 12, this);

			g2d.drawImage(images[type], 18, 18, 22, 23, null);
			g2d.drawImage(images[type], 18, 45, 22, 23, null);
			g2d.drawImage(images[type], 60, 18, 22, 23, null);
			g2d.drawImage(images[type], 60, 45, 22, 23, null);
			g2d.drawImage(images[type], 38, 30, 22, 23, null);
			g2d.translate(w, h);
			g2d.rotate(Math.PI);
			g2d.drawString("10", -1, 18);
			g2d.drawImage(images[type], 3, 25, 12, 12, this);

			g2d.drawImage(images[type], 14, 18, 22, 23, null);
			g2d.drawImage(images[type], 14, 45, 22, 23, null);
			g2d.drawImage(images[type], 56, 18, 22, 23, null);
			g2d.drawImage(images[type], 56, 45, 22, 23, null);
			g2d.drawImage(images[type], 34, 30, 22, 23, null);

			break;
		case 11:
			g2d.drawString("J", 6, 20);
			g2d.drawImage(images[type], 7, 25, 12, 12, this);
			g2d.drawImage(Resources.JAVA_IMAGE, 18, 43, 60, 60, this);
			g2d.translate(w, h);
			g2d.rotate(Math.PI);
			g2d.drawImage(images[type], 7, 25, 12, 12, this);
			g2d.drawString("J", 6, 20);
			break;
		case 12:
			g2d.drawString("Q", 6, 20);
			g2d.drawImage(images[type], 7, 25, 12, 12, this);
			g2d.drawImage(Resources.Q_IMAGE, 16, 41, 64, 64, this);
			g2d.translate(w, h);

			g2d.rotate(Math.PI);
			g2d.drawString("Q", 6, 20);
			g2d.drawImage(images[type], 7, 25, 12, 12, this);
			break;
		case 13:
			g2d.drawString("K", 6, 20);
			g2d.drawImage(images[type], 7, 25, 12, 12, this);
			g2d.drawImage(Resources.K_IMAGE, 16, 41, 64, 64, this);
			g2d.translate(w, h);
			g2d.rotate(Math.PI);
			g2d.drawString("K", 6, 20);
			g2d.drawImage(images[type], 7, 25, 12, 12, this);
			break;
		case 14:
			g2d.drawString("A", 6, 20);
			g2d.drawImage(images[type], 7, 25, 12, 12, this);
			g2d.drawImage(images[type], 37, 60, 20, 25, null);
			g2d.translate(w, h);
			g2d.rotate(Math.PI);
			g2d.drawString("A", 5, 20);
			g2d.drawImage(images[type], 7, 25, 12, 12, this);
			break;
		case 15:
			g2d.drawString("2", 6, 20);

			g2d.drawImage(images[type], 6, 22, 12, 12, this);
			g2d.drawImage(images[type], 37, 15, 20, 25, null);
			g2d.translate(w, h);
			g2d.rotate(Math.PI);
			g2d.drawString("2", 6, 20);
			g2d.drawImage(images[type], 6, 22, 12, 12, this);
			g2d.drawImage(images[type], 37, 15, 20, 25, null);
			break;
		case 16:
			g2d.setFont(new Font("Times New Roman", Font.BOLD, 13));
			g2d.drawString("J", 6, 20);
			g2d.drawString("O", 6, 30);
			g2d.drawString("K", 6, 40);
			g2d.drawString("E", 6, 50);
			g2d.drawString("R", 6, 60);
			g2d.drawImage(Resources.JOKER_IMAGE, 16, 20, 64, 104, this);
			g2d.translate(w, h);
			g2d.rotate(Math.PI);

			g2d.drawString("J", 6, 20);
			g2d.drawString("O", 6, 30);
			g2d.drawString("K", 6, 40);
			g2d.drawString("E", 6, 50);
			g2d.drawString("R", 6, 60);
			break;

		}
		// g2d.drawImage(new
		// ImageIcon("View\\bight.png").getImage(),3*getWidth()/8,2*getHeight()/5,this.getWidth()/4,this.getHeight()/5,null);

		// g2d.drawString(""+getNum(),15,25);

	}

	/**
	 * @return Returns the num.
	 */
	public int getNum() {
		return num;
	}

	/**
	 * @param num
	 *            The num to set.
	 */
	public void setNum(int num) {
		this.num = num;
	}

	/**
	 * @return Returns the type.
	 */
	public int getType() {
		return type;
	}

	public void toVertical() {
		isVertical = true;
		setSize(new Dimension(19 * 5, 29 * 5));

	}

	public void toHorizental() {
		isVertical = false;
		setSize(new Dimension(29 * 5, 19 * 5));

	}

	/**
	 * @param type
	 *            The type to set.
	 */
	public void setType(int type) {
		this.type = type;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JFrame testFrame = new JFrame(), testFrame2 = new JFrame();
		testFrame.setLayout(null);
		testFrame2.setLayout(null);
		Card card = new Card(0, 14);
		Card card2 = new Card(0, 14, false);
		card.setLocation(10, 40);
		card2.setLocation(10, 40);
		testFrame2.add(card2);
		testFrame.add(card);
		testFrame.setSize(200, 300);
		testFrame2.setSize(200, 300);
		testFrame2.setVisible(true);
		testFrame.setVisible(true);
		card.uncover();
		card2.uncover();

	}

	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		if (!covered && !sent && isVertical())
			if (selected) {
				unSelect();
			} else {
				select();
			}

	}

	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		// setBackground(Color.orange);
	}

	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		// setBackground(Color.white);
	}

	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	public int compareTo(Object arg0) {
		// TODO Auto-generated method stub
		Card toCom = (Card) arg0;
		return num - toCom.getNum();
	}

}
